package org.tea.inventory.quartz.config;


import org.tea.inventory.quartz.factory.QuartzJobFactory;
import org.tea.inventory.quartz.job.DemoJob;
import org.tea.inventory.quartz.properties.DruidConnectionProvider;
import org.tea.inventory.quartz.service.impl.QuartzServiceImpl;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.TimeZone;

/**
 * @author gourd.hu
 * @date 2018-11-20
 */
@Configuration
//@EnableScheduling
@Import({DruidConnectionProvider.class, DemoJob.class, QuartzServiceImpl.class})
public class QuartzAutoConfig {

    public static final String SYS_CHINA_TIMEZONE = "Asia/Shanghai";

    @Autowired
    private DruidConnectionProvider druidConnectionProvider;
    @Autowired
    private QuartzJobFactory jobFactory;
    private DataSource quartzDataSource;


    /**
     * 配置触发器tableKeyTrigger
     *
     * @param tableKeyJobDetail
     * @return
     */
    @Bean("tableKeyTrigger")
    public CronTriggerFactoryBean tableKeyTrigger(JobDetail tableKeyJobDetail) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(tableKeyJobDetail);
        trigger.setCronExpression("0 0/30 * * * ?");
        trigger.setTimeZone(TimeZone.getTimeZone(SYS_CHINA_TIMEZONE));
        trigger.setName("tableKeyTrigger");
        return trigger;
    }


    /**
     * 配置定时任务tableKeyJobDetail
     *
     * @return
     */
    @Bean("tableKeyJobDetail")
    public JobDetailFactoryBean tableKeyJobDetail() {
        JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
        // 任务的名字
        jobDetail.setName("demoJob");
        jobDetail.setJobClass(DemoJob.class);
        jobDetail.setDurability(true);
        return jobDetail;
    }


    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("tableKeyTrigger") Trigger tableKeyTrigger) throws IOException {
//获取配置属性
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        //创建SchedulerFactoryBean
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setQuartzProperties(propertiesFactoryBean.getObject());
        factory.setJobFactory(jobFactory);//支持在JOB实例中注入其他的业务对象
        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
        factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时，会等待所有已经启动的quartz job结束后spring才能完全shutdown。
        factory.setOverwriteExistingJobs(false);//是否覆盖己存在的Job
        factory.setStartupDelay(10);//QuartzScheduler 延时启动，应用启动完后 QuartzScheduler 再启动
        factory.setTriggers(tableKeyTrigger);
        return factory;
    }

    @Bean(name = "scheduler")
    public Scheduler scheduler(@Qualifier("tableKeyTrigger") Trigger tableKeyTrigger) throws SchedulerException, IOException {

        return schedulerFactoryBean(tableKeyTrigger).getScheduler();
    }
}
